<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部 -->
        <TreeNode :tree-node="company" />
        <!-- <TreeNode :tree-node="data" /> -->
        <!-- 树形 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <!-- 用了一个行列布局 -->
          <template slot-scope="{ data }">
            <TreeNode :tree-node="data" @delDepartments="getDepartments" />
          </template>
        </el-tree>
      </el-card>
      <!-- 新增功能的弹框 -->
      <!-- <template v-if="$store.state.departments.showDialog"> -->
      <AddDept />
      <!-- </template> -->
    </div>
  </div>
</template>

<script>
import TreeNode from './components/tree-node.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Departments',
  components: {
    TreeNode,
    AddDept
  },
  data() {
    return {
      // 单独声明变量保存头部信息
      company: { name: '', manager: '', id: '' },
      departs: [],
      defaultProps: {
        label: 'name', // 表示 从这个属性显示内容,
        children: 'children'
      },
      // 定义控制弹框的变量
      showDialog: false,
      // 定义一个存储部门信息的变量
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    async getDepartments() {
      const result = await getDepartments()
      // this.company.name = result.companyName
      // this.$forceUpdate()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      // this.departs = result.depts // 需要将其转化成树形结构
      this.departs = tranListToTreeData(result.depts, '')
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
